package com.kamm.springbootflower.action;

import com.kamm.springbootflower.domain.Flower;
import com.kamm.springbootflower.service.FlowerService;
import com.kamm.springbootflower.utils.FileTools;
import com.kamm.springbootflower.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @program: spring-boot-flower
 * @description
 * @author: 周佳佳
 * @create: 2020-03-03 13:41
 **/
@Controller
@RequestMapping("Two")
public class Two {
    @Resource
    FlowerService flowerService;

    @Resource
    FileTools fileTools;
    @RequestMapping("findById")
    @ResponseBody
    public Flower findById(Integer fid){
        Flower flower=null;
        if(!StringUtils.isEmpty(fid)){
           flower=flowerService.selectByPrimaryKey(fid);
        }else{
           flower=new Flower();
           flower.setfName("没有哦");
        }
        return flower;
    }
    @RequestMapping("addOrupdate")
    @ResponseBody
    public Message addOrupdate(Flower flower){
        Message message=new Message();
        if (flower.getFileone().getOriginalFilename() != null && !"".equals(flower.getFileone().getOriginalFilename())) {
            try {
                String file1 = fileTools.saveFile("TrainingPlan", flower.getFileone());
                flower.setfImage(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        flower.setfName("紫罗兰测试数据");
        flower.setNumber(12);
        int rows=flowerService.insert(flower);
        if(rows>0){
            message.setMsg("操作成功!");
            message.setNum(1);
        }
        return message;
    }
}

